package leetcode.code0777;

import leetcode.IDebug;
import leetcode.helper.H;
//执行用时：2 ms, 在所有 Java 提交中击败了92.03%的用户
//
//内存消耗：41.5 MB, 在所有 Java 提交中击败了74.10%的用户
//
//通过测试用例：94 / 94
public class Solution implements IDebug {

	public boolean canTransform(String start, String end) {
		int len = start.length();
		char[] st = start.toCharArray();
		char[] en = end.toCharArray();
		int p = 0;
		while (p < len) {
			char s = st[p];
			char e = en[p];
			if (s == e) {
				p++;
			} else if (s == 'R' && e == 'X') {
				int r = 1;
				p++;
				while (p < len && r > 0) {
					s = st[p];
					e = en[p];
					if (s == 'L' || e == 'L') {
						return false;
					}
					if (s == 'R')
						r++;
					if (e == 'R')
						r--;
					p++;
				}
				if (r != 0) {
					return false;
				}
			} else if (s == 'X' && e == 'L') {
				int l = 1;
				p++;
				while (p < len && l > 0) {
					s = st[p];
					e = en[p];
					if (s == 'R' || e == 'R') {
						return false;
					}
					if (s == 'L')
						l--;
					if (e == 'L')
						l++;
					p++;
				}
				if (l != 0) {
					return false;
				}
			} else {
				return false;
			}
		}
		return true;
	}

	@Override
	public void debug4() {
		H.compare(true, this.canTransform("XLXRRXXRXX", "LXXXXXXRRR"));
		H.compare(true, this.canTransform("RXXLRXRXL", "XRLXXRRLX"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
